/*
 * Copyright (c) 2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/opengl/GLSoftwareMatrixStack.h"

namespace alkes {

#if !defined(ALKES_GLES)

#define IsInternalMatrix(m) \
    ((uint8_t*)this < (uint8_t*)(m) && (uint8_t*)(m) < (uint8_t*)(this + 1))

GLSoftwareMatrixStack::GLSoftwareMatrixStack()
: projection_()
, modelview_()
{
    projection_stack_.push(&projection_);
    modelview_stack_.push(&modelview_);

    current_projection_ = &projection_;
    current_modelview_ = &modelview_;

    current_matrix_ = current_projection_;
}

GLSoftwareMatrixStack::~GLSoftwareMatrixStack()
{
    destroyStack(projection_stack_);
    destroyStack(modelview_stack_);
}

void GLSoftwareMatrixStack::destroyStack(Deque& stack)
{
    Matrix* m = NULL;

    while (NULL != (m = (Matrix*)stack.pop()))
    {
        if (IsInternalMatrix(m))
            break;

        delete m;
    }
}

void GLSoftwareMatrixStack::setMode(MatrixMode mode)
{
    current_matrix_ =
        (mode == AL_MM_PROJECTION) ? current_projection_ : current_modelview_;
}

void GLSoftwareMatrixStack::pushMatrix()
{
    Deque* stack;
    Matrix** current;
    if (current_matrix_ == current_projection_)
    {
        stack = &projection_stack_;
        current = &current_projection_;
    }
    else
    {
        stack = &modelview_stack_;
        current = &current_modelview_;
    }

    Matrix* m = new Matrix();
    memcpy(m->m_, current_matrix_->m_, sizeof(*m));

    stack->push(m);
    *current = m;
}

void GLSoftwareMatrixStack::popMatrix()
{
    if (IsInternalMatrix(current_matrix_))
        return ; // Failed!

    Deque* stack;
    Matrix** current;
    if (current_matrix_ == current_projection_)
    {
        stack = &projection_stack_;
        current = &current_projection_;
    }
    else
    {
        stack = &modelview_stack_;
        current = &current_modelview_;
    }

    Matrix* m = (Matrix*)stack->pop();
    delete m;

    *current = (Matrix*)((void*)*stack->getLast());
}

void GLSoftwareMatrixStack::identity()
{
    current_matrix_->identity();
}

void GLSoftwareMatrixStack::multiply(const Matrix& m)
{
    current_matrix_->multiply(m, *current_matrix_);
}

void GLSoftwareMatrixStack::perspective(float fovy, float aspect, float znear, float zfar)
{
    current_matrix_->perspective(fovy, aspect, znear, zfar);
}

void GLSoftwareMatrixStack::frustum(float left, float right, float bottom, float top, float znear, float zfar)
{
    current_matrix_->frustum(left, right, bottom, top, znear, zfar);
}

void GLSoftwareMatrixStack::ortho(float left, float right, float bottom, float top, float znear, float zfar)
{
    current_matrix_->ortho(left, right, bottom, top, znear, zfar);
}

void GLSoftwareMatrixStack::lookAt(const Vector3D& eye, const Vector3D& at, const Vector3D& up)
{
    current_matrix_->lookAt(eye, at, up);
}

void GLSoftwareMatrixStack::translate(float x, float y, float z)
{
    current_matrix_->translate(x, y, z);
}

void GLSoftwareMatrixStack::scale(float xscale, float yscale, float zscale)
{
    current_matrix_->scale(xscale, yscale, zscale);
}

void GLSoftwareMatrixStack::rotate(float angle, float x, float y, float z)
{
    current_matrix_->rotate(angle, x, y, z);
}

#endif

}
